package org.myy.learn.aaa.b_双指针;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class 移动零 {

    //快指针[不断往前]，快指针遇到非零， =>交换并慢指针进一

    /**
     * 主方法
     * @param nums
     */
    public static void moveZeroes(int[] nums) {
        int n = nums.length, slow = 0, fast = 0;
        while (fast < n){
            if (nums[fast] != 0){
                swap(nums, slow, fast);
                slow++;
            }
            fast++;
        }
    }

    /**
     * 数值交换
     * @param nums
     * @param left
     * @param right
     */
    public static void swap(int[] nums, int left, int right){
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

    public static void main(String[] args) {
        int[] nums = new int[]{1,3,0,0,12};
        moveZeroes(nums);
        log.info("执行结束，nums={}", nums);
    }
}
